package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.ReceiveGiftAdapter;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.data.JinbiOrderParentInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.http.engine.TaskEventListener;
import com.xgkj.diyiketang.task.ShouYiListTask;
import com.xgkj.diyiketang.weidget.MyLoadDataListView;

import java.util.List;

/**
 * Created by XCKJ_Paladowow on 2016/11/17.
 */
public class MyShouYiActivity extends BaseActivity implements MyLoadDataListView.LoadDataListener {
    private Context mContext;
    private TextView back;
    private TextView totalFlowers;
    private MyLoadDataListView receiveGiftList;
    private ReceiveGiftAdapter receiveGiftAdapter;
    private int size = 20;
    private int currentpage_r = 1;
    private int receiveTaskId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_shouyi_list);
        mContext = this;
        initView();
    }

    private void initView() {
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        totalFlowers = (TextView) findViewById(R.id.total_flowers);
        receiveGiftList = (MyLoadDataListView) findViewById(R.id.receive_gift_list);
        receiveGiftList.setOnLoadDataListener(this);
        receiveGiftAdapter = new ReceiveGiftAdapter(mContext);
        receiveGiftList.setAdapter(receiveGiftAdapter);
        getReceiveGiftList();
    }

    private void getReceiveGiftList() {
        if (receiveTaskId != -1) return;
        if (currentpage_r == 0) {
            receiveGiftList.setNoNextPage();
            return;
        }
        ShouYiListTask receiveTask = new ShouYiListTask(size, currentpage_r);
        receiveTask.addListener(mListener);
        receiveTaskId = getEatShowApp().getTaskManager().addTask(receiveTask);
    }

    private TaskEventListener mListener = new ShowToastTaskEventListener(this) {
        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (taskId == receiveTaskId) {
                receiveTaskId = -1;
                if (data != null) {
                    JinbiOrderParentInfo info = (JinbiOrderParentInfo) data;
                    List<EatShowUserInfo> list = info.getList();
                    if (currentpage_r == 1) {
                        receiveGiftAdapter.clearData();
                        receiveGiftList.enableLoadMore();
                    }
                    if (list != null && list.size() > 0) {
                        receiveGiftAdapter.updateData(list);
                    }
                    String flowers = info.getFlowers();
                    totalFlowers.setText(flowers);
                    currentpage_r = info.getCurrentPage() >= info.getPageCount() ? 0 : (info.getCurrentPage() + 1);
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (taskId == receiveTaskId) {
                receiveTaskId = -1;
                receiveGiftList.setFootProgressVisible(false);
            }
        }

        @Override
        public void handleTaskMessage(int taskId, Object data) {
            super.handleTaskMessage(taskId, data);
        }
    };

    @Override
    public void onLoadMore() {
        getReceiveGiftList();
    }
}
